using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Brick : MonoBehaviour
{
    private SpriteRenderer _spriteRenderer;
    private int health;
    [SerializeField]
    private Sprite[] sprites;
    [SerializeField]
    private GameObject ballPrefabs;
    public int score = 10;
    private Vector3 offset = new(1f, 1f, 0f);
    private void Start()
    {
        _spriteRenderer = GetComponent<SpriteRenderer>();
        health = sprites.Length;
        _spriteRenderer.sprite = sprites[health - 1];
    }
    private void CreateBall()
    {
        GameObject.Instantiate(ballPrefabs, transform.position + offset, Quaternion.identity);
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {

        if (collision.gameObject.CompareTag("Ball"))
        {
            int ram = Random.Range(0, 5);
            if (ram == 2)
            {
                CreateBall();
                FindObjectOfType<DeadGround>().NoMissTime();
            }
            health--;
            if (health <= 0)
            {
                gameObject.SetActive(false);
                FindObjectOfType<GameManager>().Hit(this);
                return;
            }
            _spriteRenderer.sprite = sprites[health - 1];
        }
    }
}
